package com.hancher.demo.hrcircleprogressbar;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.view.InputDeviceCompat;

import com.hancher.plugin.hrcircleprogressbar.R;

public class HrCircleProgressBar extends View {
    private int circleBgColor;
    private float circleBgWidth;
    private int circleColor;
    private Paint circlePaint;
    private float circleWidth;
    private float currentAngle;
    private float currentProgress;
    private int duration;
    private boolean isDefaultText;
    private float lastAngle;
    private String mTextValue;
    private float maxProgress;
    private ValueAnimator progressAnimator;
    private float section;
    private int startAngle;
    private int textColor;
    private Paint textPaint;
    private float textSize;
    private int totalAngle;

    public HrCircleProgressBar(Context context) {
        this(context, null);
    }

    public HrCircleProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HrCircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.duration = 1000;
        this.circlePaint = new Paint();
        this.textPaint = new Paint();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HrCircleProgressBar);
        this.circleColor = typedArray.getColor(R.styleable.HrCircleProgressBar_circle_color, -65536);
        this.circleBgColor = typedArray.getColor(R.styleable.HrCircleProgressBar_circle_bg_color, InputDeviceCompat.SOURCE_ANY);
        this.circleWidth = typedArray.getDimension(R.styleable.HrCircleProgressBar_circle_width, 2.0f);
        this.circleBgWidth = typedArray.getDimension(R.styleable.HrCircleProgressBar_circle_bg_width, 2.0f);
        this.textColor = typedArray.getColor(R.styleable.HrCircleProgressBar_text_color, -16776961);
        this.textSize = typedArray.getDimension(R.styleable.HrCircleProgressBar_text_size, 10.0f);
        this.totalAngle = typedArray.getInteger(R.styleable.HrCircleProgressBar_total_angle, 360);
        this.startAngle = typedArray.getInteger(R.styleable.HrCircleProgressBar_start_angle, 0);
        this.currentProgress = typedArray.getFloat(R.styleable.HrCircleProgressBar_current_progress, 0.0f);
        this.maxProgress = typedArray.getFloat(R.styleable.HrCircleProgressBar_max_progress, 100.0f);
        setCurrentProgress(this.currentProgress);
        setMaxProgress(this.maxProgress);
        typedArray.recycle();
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int centre = getWidth() / 2;
        int radius = ((int) (centre - (this.circleWidth / 2.0f))) - 2;
        this.circlePaint.setColor(this.circleBgColor);
        this.circlePaint.setStyle(Paint.Style.STROKE);
        this.circlePaint.setAntiAlias(true);
        this.circlePaint.setStrokeCap(Paint.Cap.ROUND);
        this.circlePaint.setStrokeWidth(this.circleBgWidth);
        RectF oval = new RectF((centre - radius) - 1, (centre - radius) - 1, centre + radius + 1, centre + radius + 1);
        canvas.drawArc(oval, this.startAngle, this.totalAngle, false, this.circlePaint);
        this.circlePaint.setStrokeWidth(this.circleWidth);
        this.circlePaint.setColor(this.circleColor);
        canvas.drawArc(oval, this.startAngle, this.currentAngle, false, this.circlePaint);
        this.textPaint.setAntiAlias(true);
        this.textPaint.setColor(this.textColor);
        this.textPaint.setTextSize(this.textSize);
        if (!this.isDefaultText) {
            float textWidth = this.textPaint.measureText(((int) this.currentProgress) + "");
            canvas.drawText(String.valueOf((int) this.currentProgress), centre - (textWidth / 2.0f), centre + (this.textSize / 2.0f), this.textPaint);
        } else {
            float textWidth2 = this.textPaint.measureText(this.mTextValue);
            canvas.drawText(this.mTextValue, centre - (textWidth2 / 2.0f), centre + (this.textSize / 2.0f), this.textPaint);
        }
        invalidate();
    }

    public float getMaxProgress() {
        return this.maxProgress;
    }

    public void setMaxProgress(float maxProgress) {
        if (maxProgress < 0.0f) {
            throw new IllegalArgumentException("max not less than 0");
        }
        this.maxProgress = maxProgress;
        this.section = this.totalAngle / maxProgress;
    }

    public void setAnimationDuration(int duration) {
        this.duration = duration;
    }

    public void setCurrentProgress(float progress) {
        if (progress >= 0.0f) {
            this.currentProgress = progress;
            if (progress > this.maxProgress) {
                progress = this.maxProgress;
            }
            float f = this.currentAngle;
            this.lastAngle = f;
            setAnimation(f, this.section * progress, this.duration);
        }
    }

    private void setAnimation(float last, float current, int duration) {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(last, current);
        this.progressAnimator = ofFloat;
        ofFloat.setDuration(duration);
        this.progressAnimator.setTarget(Float.valueOf(this.currentAngle));
        this.progressAnimator.addUpdateListener(new AnonymousClass1());
        this.progressAnimator.start();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.hancher.common.circleprogressbar.CirCleProgressBar$1, reason: invalid class name */
    public class AnonymousClass1 implements ValueAnimator.AnimatorUpdateListener {
        AnonymousClass1() {
        }

        @Override // android.animation.ValueAnimator.AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            HrCircleProgressBar.this.currentAngle = ((Float) valueAnimator.getAnimatedValue()).floatValue();
            HrCircleProgressBar hrCircleProgressBar = HrCircleProgressBar.this;
            hrCircleProgressBar.currentProgress = hrCircleProgressBar.currentAngle / HrCircleProgressBar.this.section;
        }
    }

    public int getCircleColor() {
        return this.circleColor;
    }

    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
    }

    public int getCircleBgColor() {
        return this.circleBgColor;
    }

    public void setCircleBgColor(int circleBgColor) {
        this.circleBgColor = circleBgColor;
    }

    public float getCircleWidth() {
        return this.circleWidth;
    }

    public void setCircleWidth(float circleWidth) {
        this.circleWidth = circleWidth;
    }

    public float getCircleBgWidth() {
        return this.circleBgWidth;
    }

    public void setCircleBgWidth(float circleBgWidth) {
        this.circleBgWidth = circleBgWidth;
    }

    public int getTextColor() {
        return this.textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public float getTextSize() {
        return this.textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public void setText(boolean isText, String text) {
        this.isDefaultText = isText;
        this.mTextValue = text;
    }
}